#!/bin/bash

#let "X_iB_cnt = $HSTA_X_GRID_CNT / 2"
#let "Y_iB_cnt = $HSTA_Y_GRID_CNT / 2"
let "X_iB_cnt = $1 / 2"
let "Y_iB_cnt = $2 / 2"
let "tot_iB_cnt = $X_iB_cnt * $Y_iB_cnt"

X_MOVE_PACE=2
Y_MOVE_PACE=2

for (( i=0; i<$tot_iB_cnt; i++ ))
do 
    temp_iB=0
    let "x_pos=$i % $X_iB_cnt"
    let "y_pos=$i / $X_iB_cnt"
    let "start_pos=1 + $x_pos*$X_MOVE_PACE + $y_pos*$Y_MOVE_PACE*$1"
    
    region_name="region"$start_pos
    data1=$(awk -v reg_name=$region_name '$1==reg_name {printf "%f", $2}' $3)
    
    let "start_pos=$start_pos+1"
    region_name="region"$start_pos
    data2=$(awk -v reg_name=$region_name '$1==reg_name {printf "%f", $2}' $3)
    
    let "start_pos=1 + $x_pos*$X_MOVE_PACE + $y_pos*$Y_MOVE_PACE*$1 + $1"
    region_name="region"$start_pos
    data3=$(awk -v reg_name=$region_name '$1==reg_name {printf "%f", $2}' $3)
    
    let "start_pos=$start_pos+1"
    region_name="region"$start_pos
    data4=$(awk -v reg_name=$region_name '$1==reg_name {printf "%f", $2}' $3)
    #echo $data1" "$data2" "$data3" "$data4
    IB_VAL=$(echo "$data1+$data2+$data3+$data4" | bc | awk '{printf "%f", $0}')
    echo $IB_VAL
    echo $IB_VAL
done